package com.zxjbyte.yiyi.module.system.controller.auth;

import com.zxjbyte.yiyi.framework.common.constant.Constant;
import com.zxjbyte.yiyi.framework.common.domain.api.R;
import com.zxjbyte.yiyi.module.system.domain.response.auth.CaptchaResponse;
import com.zxjbyte.yiyi.module.system.service.auth.ICaptchaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 基于hutool实现的验证码接口
 *
 * @Author zhangxingjia
 * @Date 2024/8/13 17:03
 * @Version: 1.0
 */
@Slf4j
@Tag(name = "验证码接口")
@RestController
@RequestMapping(value = Constant.BASE_PATH_B + "/system/captcha")
public class CaptchaController {

    @Resource
    private ICaptchaService captchaService;

    @Operation(summary = "生成验证码")
    @GetMapping("/generate")
    public R<CaptchaResponse> generateCaptcha(HttpSession session) {
        return R.data(captchaService.generateCaptcha(session));
    }

    @Operation(summary = "验证验证码")
    @GetMapping("/verify")
    public R<?> verify(HttpServletRequest request, @RequestParam String uuid, @RequestParam String code) {
        return captchaService.verify(request, uuid, code) ? R.ok() : R.fail("验证码错误或已过期！");
    }
}
